GdPicture.NET.14.API
GdPicture14 Namespace / GdPicturePDF Class / AddJpegImageFromStream Method / AddJpegImageFromStream(Stream,Byte[]) Method
Example





In This Topic
AddJpegImageFromStream(Stream,Byte[]) Method
In This Topic
Creates an image resource directly from your JPEG image file without the need of lossy encoding and decoding process including option to provide ICC profile data. The image resource is subsequently added into the currently loaded PDF document. You can use this resource straightforward, for example, with the GdPicturePDF.DrawImage method. This process allows you to easily draw an image from your JPEG file onto a page within the currently loaded PDF document.
Syntax
'Declaration
 
Public Overloads Function AddJpegImageFromStream( _
   ByVal Stream As Stream, _
   ByVal ProfileData() As Byte _
) As String
public string AddJpegImageFromStream( 
   Stream Stream,
   byte[] ProfileData
)
public function AddJpegImageFromStream( 
    Stream: Stream;
    ProfileData: Bytearray of
): String; 
public function AddJpegImageFromStream( 
   Stream : Stream,
   ProfileData : byte[]
) : String;
public: string* AddJpegImageFromStream( 
   Stream* Stream,
   byte[]* ProfileData
) 
public:
String^ AddJpegImageFromStream( 
   Stream^ Stream,
   array<byte>^ ProfileData
) 

Parameters

Stream
ProfileData

Return Value

Returns a name of the newly created image resource strictly bounded to the currently loaded PDF document. The GdPicturePDF.GetStat method can be subsequently used to determine if this method has been successful.

This name can be subsequently used with the GdPicturePDF.DrawImage method to draw an image onto the page. For the correct usage please see the example below.

Remarks
This method is only allowed for use with non-encrypted documents.

It is recommend to use the GdPicturePDF.GetStat method to identify the specific reason for the method's failure, if any.

Example
How to draw a JPEG image from a file onto a new page and save it to a PDF file.
Dim caption As String = "Example: AddJpegImageFromFile"
Using gdpicturePDF As New GdPicturePDF()
    Dim status As GdPictureStatus = gdpicturePDF.NewPDF()
    If status = GdPictureStatus.OK Then
        Dim image_name As String = gdpicturePDF.AddJpegImageFromFile("image.jpg")
        status = gdpicturePDF.GetStat()
        If status = GdPictureStatus.OK Then
            status = gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4)
            If status <> GdPictureStatus.OK Then
                Return
            End If
            gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft)
            gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
            status = gdpicturePDF.DrawImage(image_name, 0, 0, 210, 297)
            If status <> GdPictureStatus.OK Then
                Return
            End If
            status = gdpicturePDF.SaveToFile("Test_AddJpegImage.pdf")
            If status = GdPictureStatus.OK Then
                MessageBox.Show("Your image has been successfully saved to a pdf file.", caption)
            Else
                MessageBox.Show("The file can't be saved. Status: " + status.ToString(), caption)
            End If
        Else
            MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + status.ToString(), caption)
        End If
    Else
        MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption)
    End If
End Using
string caption = "Example: AddJpegImageFromFile";
using (GdPicturePDF gdpicturePDF = new GdPicturePDF())
{
    GdPictureStatus status = gdpicturePDF.NewPDF();
    if (status == GdPictureStatus.OK)
    {
        string image_name = gdpicturePDF.AddJpegImageFromFile("image.jpg");
        status = gdpicturePDF.GetStat();
        if (status == GdPictureStatus.OK)
        {
            status = gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4);
            if (status != GdPictureStatus.OK) return;
            gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);
            gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
            status = gdpicturePDF.DrawImage(image_name, 0, 0, 210, 297);
            if (status != GdPictureStatus.OK) return;
            status = gdpicturePDF.SaveToFile("Test_AddJpegImage.pdf");
            if (status == GdPictureStatus.OK)
            {
                MessageBox.Show("Your image has been successfully saved to a pdf file.", caption);
            }
            else
            {
                MessageBox.Show("The file can't be saved. Status: " + status.ToString(), caption);
            }
        }
        else
        {
            MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + status.ToString(), caption);
        }
    }
    else
    {
        MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption);
    }
}
See Also